package list;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : ListDemo
 * @Author : 54061
 * @Date: 2022/2/12 14:09
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class ListDemo {
    public static void main(String[] args) {
        //多态创对象
        List<String> list = new ArrayList<>();
        System.out.println("list = " + list);
        //添加元素
        list.add("张三丰");
        list.add("张翠山");
        list.add("殷素素");
        list.add("张无忌");
        list.add("赵敏");
        System.out.println("list = " + list);
        //插入元素
        list.add(1,"灭绝师太");
        System.out.println("list = " + list);
        //删除殷素素
        list.remove(3);
        //[张三丰, 灭绝师太, 张翠山, 张无忌, 赵敏]
        System.out.println("list = " + list);
        //把赵敏修改成 小昭
        System.out.println("list.set(4,\"小昭\") = " + list.set(4, "小昭"));
        System.out.println("list = " + list);
        //获取元素 get
        System.out.println("list.get(3) = " + list.get(3));
        System.out.println("list = " + list);
        System.out.println("----------------------------------");
        //转数组
        Object[] objs = list.toArray();
        for (Object obj : objs) {
            System.out.println("obj = " + obj.toString());//元素值
        }
        System.out.println("----------------------------------");
        //迭代器
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            String name = it.next();
            System.out.println("name = " + name);
        }
        System.out.println("----------------------------------");
        //增强for
        for (String name : list) {
            System.out.println("name = " + name);
        }
        System.out.println("----------------------------------");
        //普通for
        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i);
            System.out.println("name = " + name);
        }
        System.out.println("----------------------------------");
        //列表迭代器
        ListIterator<String> lit = list.listIterator();
        while(lit.hasNext()){
            String name = lit.next();
            System.out.println("name = " + name);
        }
    }
}
